<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Caipu extends CF_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->driver('cache', array('adapter' => 'file'));
		$this->cachtime = 86400;
		$this->load->model('caipu_mdl');
		$this->uid = $this->user_login->is_logged_in() ? $this->user_login->get_user_id() : 0;
	}

	public function index()
	{
		$this->created();
	}

	/**
	 * 菜谱内容页
	 * @param  string $id [description]
	 * @return [type]     [description]
	 */
	public function recipe($id = '', $print = NULL)
	{
		$this->load->helper('text');
        $this->load->model('comments_mdl');

		/** 如为空，则跳转 */
		if (empty($id) || !is_numeric($id)) redirect(site_url());

		/** 获取菜谱信息 */
		$caipu_info = $this->caipu_mdl->get_by(array('id'=>$id));

		/** 不存在? */
		if (!$caipu_info) show_404();

		$data['comments'] = $this->comments_mdl->get_cmts($id, 'caipu', 'approved', 5, 0, 'ASC')->result_array();

		/** 内容显示格式化 */
		$caipu_info = $this->_prepare_caipu_info($caipu_info);

		/** 更新浏览次数 */
		$this->caipu_mdl->update_views($id);

		$data['caipu_info'] = $caipu_info;

		switch ($print)
		{
			case 'print':
				$this->load->view('caipu_print',$data);
			break;
			case 'image':
				$zuofa = $yongliao = $zuofaimg = '';
				$i = 0;
				foreach ($data['caipu_info']['materials'] as $materials_list) {
					$materials = explode('，',$materials_list);
					$yongliao .= @$materials[0].':'.@$materials[1].'; ';
				}

				foreach ($data['caipu_info']['setpInfos'] as $setpInfos) {
					$i++;
					if($setpInfos != ''){
						if (@$data['caipu_info']['setpimage'][$i]['location']){
							$zuofaimg = '<img src="'.@$data['caipu_info']['setpimage'][$i]['location'].'"/>';
						}
						$zuofa .= $zuofaimg.'<br/>'.$i.'.'.$setpInfos.'<br/>';
					}
				}

				$data_img['input'] = '<br/>'.$data['caipu_info']['title'].'<hr/><img src="'.base_url($data['caipu_info']['coverImage']).'" /> <hr/> <b>用料：</b>'.$yongliao.'<hr/><b>做法：</b><br/>'.$zuofa.'<hr/><b>小贴士：</b><br/>'.$data['caipu_info']['tips'].'';

				$this->load->library('html2img',$data_img);
			break;
			default:
				$data['article_list'] = $this->_cache_article_list();
				$this->load->view('caipu_recipe',$data);
		}
	}

	public function created($class_name = 'all', $page = '')
	{
    $this->load->model('Shicai_mdl');
		$class_name = urldecode($class_name);
		$page = max(1, intval($page));
		$limit = 21; //每页显示数
		//列表页虚拟更新 缓存一组随机数，临时用 不做最终解决方案
		$offset = ($page - 1) * $limit + ( ($class_name == 'all') ? $this->_cache_created_rand() : 0 ); 
    //WAP分页
    if ($this->input->get('offset') && $this->input->get('limit')) 
    {
      $offset = $this->input->get('offset');
      $limit = $this->input->get('limit');
    } 

		$condition = $class_name != 'all' ? array('class_name LIKE'	=>	'%'.$class_name.'%','status'	=>	1) : array('status'	=>	1);
		$caipu_array = $this->caipu_mdl->get($condition, $offset, $limit)->result_array();

		$rows_array = array();
		foreach ($caipu_array as $value) 
		{
			$value['coverImage'] = server_img($value['coverImage']);
			$value['author'] = get_rand_name($value['id']);
			
			$rows_array[] = $value;
		}
		$data['caipu_created'] = $rows_array;


		$data_count = 50000; 
		if ($class_name == 'all')
		{
			$data['caipu_class'] = '菜谱大全_家常菜谱';
		}
		else
		{
			$data['caipu_class'] = $class_name.'菜谱';
		}
		
		/* 分页配置 */
		$this->load->library('pagination');
		$config['base_url'] = site_url('caipu/'.$class_name.'/');
		$config['total_rows'] = $data_count;
		$config['per_page'] = $limit; 

		$this->pagination->initialize($config); 
		
		$this->load->view('caipu',$data);
	}

	/**
	 * 排行榜
	 * @return [type] [description]
	 */
	public function ranking()
	{

	}

	/**
	 * 菜单
	 * @return [type] [description]
	 */
	public function caidan()
	{

	}


	private function _prepare_caipu_info($caipu_info)
	{
		$this->load->helper('text');
		$this->load->model('user/user_favorite','ufavorite');
		$this->load->model('user/user_guanzhu','uguanzhu');
		$id = $caipu_info['id'];
		$authorid = $caipu_info['authorid'];
		$caipu_info['class_name'] = array_filter(explode(',', $caipu_info['class_name']));
		$caipu_info['materials'] = array_filter(explode('|', $caipu_info['materials']));
		$caipu_info['setpInfos'] = explode('|', $caipu_info['setpInfos']);

		/** 获取发布菜谱的用户信息 */
		// $this->load->model('user/users','users');
		// $caipu_by_userinfo = $this->users->get_user_by_id($caipu_info['authorid'])->row();
		$caipu_info['user_avartar'] = $this->user_login->get_avatar_dir($authorid); 
		//统一图片地址为相对路径
		$caipu_info['coverImage'] = server_img($caipu_info['coverImage']);
		/** 步骤图 */
		$this->load->model('caipu_attach_mdl');
		$data_array = array('caipu_id'=>$id,'type'=>'setp');

		$setpimage_array = $this->caipu_attach_mdl->get($data_array, NULL, NULL, 'id ASC')->result_array();
		$setpimage_rows_array = array();
		foreach ($setpimage_array as $value) 
		{
			$value['location'] = server_img($value['location']);
			$setpimage_rows_array[] = $value;
		}
		$caipu_info['setpimage'] = $setpimage_rows_array;
		
		$caipu_info['tips'] = str_replace(chr(10),'<br>', $caipu_info['tips']);
		/** 他们收藏了这个菜谱 */
		$caipu_info['fav_user'] = $this->ufavorite->get_caipu_fav_user($id, 6)->result_array();
		//$caipu_info['fav_user_num'] = $this->ufavorite->get_caipu_fav_user($id)->num_rows();
		
		/** 判断是否收藏 */
		$caipu_info['is_caipu_fav'] = $this->ufavorite->get_caipu_favorite($id, $this->uid);


		/** 判断是否关注 */
		$caipu_info['is_guanzhu'] = (0 == $this->uguanzhu->get_user_guanzhu($authorid, $this->uid)) ? FALSE : TRUE;

		/** 作者的其他菜谱 */
		$authorid_caipu_array = $this->caipu_mdl->get(array('authorid'=> $authorid), 0, 4)->result_array();
		$authorid_rows_array = array();
		foreach ($authorid_caipu_array as $value) 
		{
			$value['coverImage'] = server_img($value['coverImage']);
			$authorid_rows_array[] = $value;
		}
		$caipu_info['caipu_list_by_uid'] = $authorid_rows_array;

		/** 相关菜谱 */
		$related_caipu_array = $this->caipu_mdl->get_caipu_related('', 4)->result_array();
		$related_rows_array = array();
		foreach ($related_caipu_array as $value) 
		{
			$value['coverImage'] = server_img($value['coverImage']);
			$related_rows_array[] = $value;
		}
		$caipu_info['caipu_related_list'] = $related_rows_array;
		
		return $caipu_info;
	}

	//列表页虚拟更新 缓存一组随机数，临时用 不做最终解决方案
	public function _cache_created_rand()
	{
		$cachestr = 'list_created_rand';
		$cache = $this->cache->get($cachestr);
		if(!$cache){
			$data = mt_rand(1,999);
			$this->cache->save($cachestr,$data,$this->cachtime);
		}
		return $cache;
	}
	//文章调用方法 缓存 与首页控制器内方法，名称 相同 优化项
	public function _cache_article_list()
	{
		$cachestr = 'index_article_list';
		$cache = $this->cache->get($cachestr);
		if(!$cache){
			$this->load->model('article_mdl');
			$data = $this->article_mdl->get(array('cid !='=>'123'), 0, 7)->result_array();
			foreach($data as $item){
				$cache[] = $item;
			}
			$this->cache->save($cachestr,$data,$this->cachtime);
		}
		return $cache;
	}
}

/* End of file caipu.php */
/* Location: ./application/controllers/caipu.php */